Hi, i am making a program that reads a file from .txt and print them out using linked list. However, i need to sort them from the highest price to lowest price. I have an example here but i dont really understand, it there anyway to make it simpler?
you might want to skip straight to the bottom part of the code 1st. the top parts are just the functions that the example given used and some of my functions which im required to use.
Code:
/* my structs */
typedef struct{
Node *head;
Node *tail;
Node *iterator;
int size;
} List;
typedef struct {
char *id;
double weight;
double shippingPrice;
}Container;
typedef struct Node {
void *data;
struct Node *next;
} Node;
/* my functions */
void nd_setNext( Node *node, Node *next)
{
node->next = next;
}
Node *nd_getNext( Node *node)
{
return node->next;
}
void *nd_getData( Node *node )
{
return node->data;
}
void *lst_next(List *lst)
{
if(lst->iterator != NULL)
{
lst->iterator = node_getNext(lst->iterator);
if(lst->iterator != NULL)
return node_getData(lst->iterator);
}
return NULL;
}
void *lst_first(List *lst)
{
lst->iterator = lst->head;
if(lst->iterator != NULL)
return node_getData(lst->iterator);
else
return NULL;
}
void lst_add( List *lst, void *data)
{
lst_append(lst, data);
}
void lst_append( List *lst, void *data)
{
Node *newCnrNode = node_new(data, NULL);
if (lst->size == 0)
lst->head = newCnrNode;
else
lst->tail->next = newCnrNode;
lst->tail = newCnrNode;
lst->size++;
}
List *lst_new(void)
{
List *list = (List *)malloc(sizeof(List));
list->head = NULL;
list->tail = NULL;
list->iterator = NULL;
list->size = 0;
return list;
}
/* functions that i have to use in the sort(high to low) function */
double cnr_getShippingPrice(Container * contain)
{
return contain->shippingPrice;
}
/* functions used by the example given */
int compareOrbits(const void *plnt1, const void *plnt2)
{
int b1 = plnt_getOrbit((const Planet *)plnt1);
int b2 = plnt_getOrbit((const Planet *)plnt2);
return b1 - b2;
}
void orderPlanets(List **list)
{
Planet * tempPlanet;
List *sorted = lst_new();
for (tempPlanet = lst_first(*list);
tempPlanet != NULL;
tempPlanet = lst_next(*list))
{
lst_insertInOrder(sorted, tempPlanet, compareOrbits); /*this function that i dont understand */
}
lst_delete(*list);
*list = sorted;
}
/* part which i dont understand */
void lst_insertInOrder(List *lst, void *data,
int(*compare)(const void *data1, const void *data2))
{
Node *current = lst->head, *previous = NULL;
int smaller;
if(lst_isEmpty(lst))
lst_add(lst, data);
else
{
Node * newNode = nd_new(data, NULL);
current = lst->head;
smaller = compare(data, nd_getData(current)) < 0;
while(nd_getNext(current) != NULL && !smaller) /* nd_getNext is actually the node_getNext shown above */
{
previous = current;
current = nd_getNext(current);
smaller = compare(data, nd_getData(current)) < 0; /* ???? */
}
if(previous == NULL && smaller)
{
/* Then new node is head */
nd_setNext(newNode, lst->head);
lst->head = newNode;
}
else if( smaller)
{
nd_setNext(newNode, nd_getNext(previous));
nd_setNext(previous, newNode);
}
else
{
lst->tail->next = newNode;
lst->tail = newNode;
}
lst->size++;
}
}
i know its long but im afraid that i might miss out some things.